﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Cms.Model;
using Portal.Utility;
using Portal.Web.Model;
using Portal.Core;
using dtSearch32Lib;
using DocumentSearchServiceModel;
using DocumentSearch32Lib;
using Portal.Systems.Model;
using Portal.Workflow.Bussiness;
using Portal.Cms.Utility;
using System.Data;

namespace Portal.Cms.Render
{
    public class CmsViewSurveyResultRenderBll
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string SurveyId = (string)WebEnvironments.Request(SiteId, "gid");
                string StudentSurveyId = (string)WebEnvironments.Request(SiteId, "StudentSurveyId");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, SurveyId);
                if (OGroupData == null) throw new Exception("Tham số không hợp lệ!");
                CmsDataCls[] SubDatas = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingCmsDataForVote(SiteId, OGroupData.GroupDataId, null);
                string Html =
                   " <input type=hidden id=txtSurveyId=\"" + OGroupData.GroupDataId + "\">\r\n" +
                   " <div class=hello>Xem kết quả chương trình đánh giá phiếu điều tra khảo sát</span></div>\r\n" +
                   " <h3>" + OGroupData.GroupDataName + "<input onclick=\"window.open('SurveyResultList.aspx?gid=" + SurveyId + "','_self');\" type=button class=button value=\"Xem danh sách sinh viên đánh giá\" style=\"float:right;margin-right:4px;\"></h3>\r\n";
                if (!string.IsNullOrEmpty(OGroupData.GroupDataDescription))
                {
                    Html +=
                   "    <div class=votedesc>" + OGroupData.GroupDataDescription + "</div>\r\n";
                }
                int MaxItems = 0;
                Html +=
                   "    <input type=hidden id=\"txtMaxVoteQuestion\" value=\"" + SubDatas.Length + "\">\r\n" +
                   "    <div class=VoteBody>\r\n";
                for (int iIndex = 0; iIndex < SubDatas.Length; iIndex++)
                {
                    if (string.IsNullOrEmpty(SubDatas[iIndex].VoteType))
                    {
                        SubDatas[iIndex].VoteType = "one";
                    }
                    string ErrorText = "Xin vui lòng nhập ý kiến của bạn";
                    string Guider = "";
                    if (SubDatas[iIndex].VoteType.Equals("one"))
                    {
                        ErrorText = "Bạn chưa chọn phương án nào. Vui lòng chọn 1 phương án";
                        Guider = "<div class=CssGuide>Chỉ chọn 1 phương án</div>\r\n";
                    }
                    if (SubDatas[iIndex].VoteType.Equals("two"))
                    {
                        ErrorText = "Bạn chưa chọn phương án nào";
                        Guider = "<div class=CssGuide>Chọn nhiều phương án</div>\r\n";
                    }

                    Html += "<div class=row>\r\n" +
                          "     <h4>Câu " + (iIndex + 1) + ": " + SubDatas[iIndex].Subject + "</h4>\r\n" +
                          Guider +
                        //"     <input type=hidden id=\"txtSubject" + iIndex + "\" value=\"" + SubDatas[iIndex].Subject + "\">\r\n" +
                          "     <div id=\"divErrorItem" + iIndex + "\" class=CssError style=\"display:none;\">" + ErrorText + "</div>\r\n";
                    if (!string.IsNullOrEmpty(SubDatas[iIndex].Description))
                    {
                        Html += "<div class=cssdesc>" + SubDatas[iIndex].Description + "</div>\r\n";
                    }
                    Html += "";
                    //"<input type=hidden id=\"txtVoteType" + iIndex + "\" value=\"" + SubDatas[iIndex].VoteType + "\">\r\n" +
                    //"<input type=hidden id=\"txtVoteQuestionId" + iIndex + "\" value=\"" + SubDatas[iIndex].DataId + "\">\r\n";

                    if (SubDatas[iIndex].VoteType.Equals("text"))
                    {
                        //Html += "<textarea id=\"txtAnswerText" + iIndex + "\" class=textarea type=textarea></textarea>\r\n";
                        MaxItems++;//text tăng lên 1
                    }
                    else
                    {
                        CmsDataCls[] Ans = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingCmsDataForVote(SiteId, OGroupData.GroupDataId, SubDatas[iIndex].DataId);
                        //Html +=
                        //    "<input type=hidden id=\"txtMax" + iIndex + "\" value=\"" + Ans.Length + "\">\r\n";

                        if (Ans.Length > 0)
                        {
                            Html +=
                                "<ul>\r\n";
                            for (int jIndex = 0; jIndex < Ans.Length; jIndex++)
                            {
                                Html += " <li>\r\n" +
                                    //"       <input type=hidden id=\"txtVoteDataSubject" + iIndex.ToString() + jIndex.ToString() + "\" value=\"" + Ans[jIndex].Subject + " (" + Ans[jIndex].TotalVote.ToString("#,##0") + ")\">\r\n" +
                                    //"       <input type=hidden id=\"txtAnswerId" + iIndex.ToString() + jIndex.ToString() + "\" value=\"" + Ans[jIndex].DataId + "\"><input id=\"chkAnswer_" + Ans[jIndex].DataId + "\" type=checkbox class=votecheck onchange=\"javascript:ProcessCheck('" + iIndex + "','" + SubDatas[iIndex].VoteType + "','" + SubDatas[iIndex].DataId + "','" + Ans[jIndex].DataId + "');\">" + Ans[jIndex].Subject +
                                                Ans[jIndex].Subject + " (" + Ans[jIndex].TotalVote.ToString("#,##0") + ")" +
                                        " </li>\r\n";
                            }
                            Html += "</ul>\r\n";
                        }
                        MaxItems = MaxItems + Ans.Length;
                    }

                    Html +=
                          "</div>\r\n";
                }
                Html +=
                    //"<input type=\"hidden\" id=\"txtMaxItems\" value=\"" + MaxItems + "\">\r\n" +
                        "<div class=rowfunc><input type=button class=button value=\"Quay lại\" onclick=\"javascript:window.open('GroupData.aspx?datatype=vote','_self');\"></div>\r\n";

                Html +=
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
